import qs from 'qs';
import ServiceBase from '../base';
import * as proxy from '../proxy';

const GET_TOKEN = '/auth/authorization';
const CHECK_TOKEN = '/authentication/v3/oauth/check_token';
const REFRESH_TOKEN = '/authentication/v3/oauth/token';
const POST_USERS = '/users';

class AuthService extends ServiceBase {
    getOauth2 = () => {
        let promise = super.get({
            url: GET_TOKEN,
        });
        return proxy.httpResultProxy(promise);
    };
    getOauth2Token = (code) => {
        let promise = super.get({
            url: `/auth/handleCode?code=` + code,
        });
        return proxy.httpResultProxy(promise);
    };
    signupUser = (signupData) => {
        let promise = super.post({
            url: POST_USERS,
            data: signupData,
        });
        return proxy.httpResultProxy(promise);
    };
    checkOAUth2Token = (checkData) => {
        let promise = super.post({
            url: CHECK_TOKEN,
            data: qs.stringify(checkData),
        });
        return proxy.httpResultProxy(promise);
    };
    refreshOauth2Token = (refreshData) => {
        let promise = super.post({
            url: REFRESH_TOKEN,
            data: qs.stringify(refreshData),
        });
        return proxy.httpResultProxy(promise);
    };
    reg = (data) => {
        let promise = super.post({
            url: POST_USERS,
            data,
        });
        return proxy.httpResultProxy(promise);
    };
    getUserInfo = (data) => {
        let promise = super.post({
            url: POST_USERS + `/${data}`,
        });
        return proxy.httpResultProxy(promise);
    };
    logout = (data) => {
        let promise = super.get({
            url: '/oauth/logout',
            params: data,
        });
        return proxy.httpResultProxy(promise);
    };
    // password token  POST /auth/password
    getToken = (data) => {
        let promise = super.postWithoutToken({
            url: `/auth/password?captchaKey=` + data.captchaKey,
            data: data.data,
        });
        return proxy.httpResultProxy(promise);
    };
    getCaptcha = () => {
        let promise = super.getWithoutToken({
            url: `/auth/captcha`,
        });
        return proxy.httpResultProxy(promise);
    };
    refreshToken = (data) => {
        let promise = super.postWithoutToken({
            url: `/auth/refresh`,
            data,
        });
        return proxy.httpResultProxy(promise);
    };
    exchangeSignature = (data) => {
        const promise = super.post({
            url: `/token/exchange`,
            data: data.data,
        });
        return proxy.httpResultProxy(promise);
    };
    // 发送邮箱消息接口
    postMail = (data) => {
        const promise = super.postWithoutToken({
            url: `/mail/code`,
            data: data.data,
        });
        return proxy.httpResultProxy(promise);
    };
    // 发送短信消息接口
    postSMS = (data) => {
        const promise = super.postWithoutToken({
            url: `/sms/code`,
            data: data.data,
        });
        return proxy.httpResultProxy(promise);
    };
    // 获取部署环境信息
    getEnvinfo = (data) => {
        const promise = super.getWithoutToken({
            url: `/envinfo?product=` + data.product,
        });
        return proxy.httpResultProxy(promise);
    };
    // 解析秘钥串
    parseSecretKey = (data) => {
        const promise = super.postWithoutToken({
            url: `/secretkey/parse`,
            data: data,
        });
        return proxy.httpResultProxy(promise);
    };
    // 激活
    activate = (data) => {
        const promise = super.postWithoutToken({
            url: `/activate`,
            data: data,
        });
        return proxy.httpResultProxy(promise);
    };
    // 获取激活记录
    listActivations = (data) => {
        const promise = super.get({
            url: `/activations?product=` + data.product,
        });
        return proxy.httpResultProxy(promise);
    };
}

export {AuthService};
